package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.BatchFlowBillDao;
import com.xbongbong.saas.domain.entity.BatchFlowBillEntity;
import com.xbongbong.saas.domain.entity.BatchFlowBillEntityExt;
import com.xbongbong.saas.domain.entity.BatchFlowBillUpdateEntity;
import com.xbongbong.saas.model.BatchFlowBillModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 批次流水model
 *
 * @author feng.zheng
 **/
@Service("batchFlowBillModel")
public class BatchFlowBillModelImpl implements BatchFlowBillModel {
    @Resource
    private BatchFlowBillDao batchFlowBillDao;
    /**
     *
     * 根据索引查询数据
     *
     * @param param 查询参数
     * @return List<ProductStockInfoEntity>
     * @author lee
     * @date 2020/1/13 16:51
     * @since v1.0
     * @version v1.0
     **/
    @Override
    public List<BatchFlowBillEntity> findEntitys(Map<String, Object> param) {
        return batchFlowBillDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return batchFlowBillDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 入库单产品数据插入
     * @param batchFlowBillEntity 供应商实体
     * @author lee
     * @date 2020/1/13 16:51
     * @since v1.0
     * @version v1.0
     * @return Integer
     */
    @Override
    public Integer insert(BatchFlowBillEntity batchFlowBillEntity) {
        batchFlowBillEntity.setAddTime(DateTimeUtil.getInt());
        batchFlowBillEntity.setUpdateTime(DateTimeUtil.getInt());
        return batchFlowBillDao.insert(batchFlowBillEntity);
    }
    /**
     * 入库单产品数据更新
     * @param batchFlowBillEntity 供应商实体
     * @author lee
     * @date 2020/1/13 16:51
     * @since v1.0
     * @version v1.0
     * @return Integer
     */
    @Override
    public Integer update(BatchFlowBillEntity batchFlowBillEntity) {
        batchFlowBillEntity.setUpdateTime(DateTimeUtil.getInt());
        return batchFlowBillDao.update(batchFlowBillEntity);
    }
    /**
     * 入库单产品数据保存
     * @param batchFlowBillEntity 保存实体
     * @author lee
     * @date 2020/1/13 16:51
     * @since v1.0
     * @version v1.0
     * @throws XbbException 异常
     * @return Integer
     */
    @Override
    public Integer save(BatchFlowBillEntity batchFlowBillEntity) {
        if (batchFlowBillEntity.getId() == null || batchFlowBillEntity.getId().equals(0L)) {
            return insert(batchFlowBillEntity);
        }
        return update(batchFlowBillEntity);
    }

    @Override
    public void insertBatch(List<BatchFlowBillEntity> batchFlowBillEntityList) throws XbbException {
        batchFlowBillDao.insertBatch(batchFlowBillEntityList);
    }

    @Override
    public Integer updateGreaterThanTime(Collection<BatchFlowBillUpdateEntity> updateList, String corpid, Set<String> batchKeyList) {
        return batchFlowBillDao.updateGreaterThanTime(updateList, corpid, batchKeyList);
    }

    @Override
    public List<BatchFlowBillEntity> getLatestFlowBillByBatchKey(String corpid, Set<String> batchKeySet, Long dateTime) {
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put(ParameterConstant.CORPID, corpid);
        params.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        params.put("lessEqualThanDate", dateTime);
        params.put("orderByStr", " date desc, id desc");
        params.put("batchKeyIn", batchKeySet);
        return batchFlowBillDao.getLatestFlowBillByWarehouseKey(params);
    }

    @Override
    public List<String> getLatestDistinctBatch(String corpid, String batchLike) {
        return batchFlowBillDao.getLatestDistinctBatch(corpid, batchLike);
    }

    @Override
    public Integer deleteByRefIdIn(String corpid, List<Long> refIdIn, List<Integer> businessTypeIn) {
        return batchFlowBillDao.deleteByRefIdIn(corpid, refIdIn, businessTypeIn);
    }

    @Override
    public List<BatchFlowBillEntityExt> getFlowInfo(Map<String, Object> param) {
        return batchFlowBillDao.getFlowInfo(param);
    }

    @Override
    public Integer getGroupByEntitysCount(Map<String, Object> param) {
        return batchFlowBillDao.getGroupByEntitysCount(param);
    }

    @Override
    public void updateBatch(Collection<BatchFlowBillUpdateEntity> updateList, String corpid, Set<String> batchKeyList) {
        batchFlowBillDao.updateBatch(updateList, corpid, batchKeyList);
    }

    @Override
    public void deleteAllByCorpId(String corpid) {
        batchFlowBillDao.deleteAllByCorpId(corpid);
    }

    @Override
    public void updateBatchByParentId(String corpid, Long parentId, Integer visible) {
        batchFlowBillDao.updateBatchByParentId(corpid, parentId, visible);
    }

    @Override
    public List<BatchFlowBillEntity> queryBatchFlowbydate(Map<String, Object> param) {
        return batchFlowBillDao.queryBatchFlowbydate(param);
    }

    @Override
    public void updateBatchFlowById(List<BatchFlowBillEntity> batchFlowBillEntityList, String corpid){
        batchFlowBillDao.updateBatchFlowById(batchFlowBillEntityList, corpid);
    }

    @Override
    public List<BatchFlowBillEntity> queryBatchFlowbydateAndId(String corpid, Long date, List<Long> productId){
        if (CollectionsUtil.isEmpty(productId)){
            return new ArrayList<>();
        }
        return batchFlowBillDao.queryBatchFlowbydateAndId(corpid, date, productId);
    }

    @Override
    public void deleteByAddTime(Map<String, Object> param) {
        batchFlowBillDao.deleteByAddTime(param);
    }

    @Override
    public List<BatchFlowBillEntity> getBatchFlowByBatchKey(String corpid, List<String> batchKeyList){
        return batchFlowBillDao.getBatchFlowByBatchKey(corpid, batchKeyList);
    }

    @Override
    public BatchFlowBillEntity getBatchFlowBillBytime(String corpid, Long productId, Long warehouseId, String batchKey){
        return batchFlowBillDao.getBatchFlowBillBytime(corpid, productId, warehouseId, batchKey);
    }
}
